using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;

namespace Acms.AcmsControls
{
    public class TextContentControlDisplay : BaseContentControlDisplay<TextContentControlProperties>
    {
        public string Text { get; set; }
        public bool AllowLineBreaks { get; set; }
        public override void SetDevProperties()
        {
            Text = "Text Content Here";
        }

        public override void PropertiesLoaded(TextContentControlProperties properties)
        {
            if (properties != null)
            {
                this.Text = properties.Text;
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (!string.IsNullOrEmpty(Text))
            {
                if (!string.IsNullOrEmpty(WrapperTag))
                    writer.RenderBeginTag(WrapperTag);
                writer.Write(Highlight(ReplaceLineBreaks(Text)));
                if (!string.IsNullOrEmpty(WrapperTag))
                    writer.RenderEndTag();
            }
        }

        private string ReplaceLineBreaks(string text)
        {
            if (AllowLineBreaks)
                return text.Replace("\n", "<br/>");
            else
                return text;
        }

        public override bool IsEmpty
        {
            get 
            {
                return string.IsNullOrEmpty(Text);
            }
        }

        protected override object SaveViewState()
        {
            ViewState["Text"] = Text;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            Text = ViewState["Text"] as string;
        }

        private string wrapperTag;
        public string WrapperTag
        {
            get { return wrapperTag; }
            set { wrapperTag = value; }
        }

        private string Highlight(string text)
        {
            if (text != null)
            {
                Control current = this;
                TemplateUserControl container = null;
                while (container == null && current != null && current.NamingContainer != null)
                {
                    current = current.NamingContainer;
                    container = current as TemplateUserControl;
                }
                if (container != null && !string.IsNullOrEmpty(container.HighlightText))
                {
                    // Setup the regular expression and add the Or operator.
                    Regex regExp = new Regex(string.Concat("\\b", container.HighlightText.Replace(" ", "|").Trim()), RegexOptions.IgnoreCase);
                    // Highlight keywords by calling the delegate each time a keyword is found.
                    return regExp.Replace(text, new MatchEvaluator(ReplaceKeyWords));
                }
                else
                {
                    return text;
                }
            }
            else
            {
                return text;
            }
        }

        private string ReplaceKeyWords(Match m)
        {

            return "<span class=\"searchHighlight\">" + m.Value + "</span>";

        }

    }
}
